package com.mycompany.xxy_dome.view;

import com.mycompany.xxy_dome.controller.ImgController;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

public class Main extends JFrame {

    private static final int FRAME_WIDTH = 100;
    private static final int FRAME_HEIGHT = 100;
    private static int CHANGE_TIME = 10000;
    private static int UI_WIDTH;
    private static int UI_HEIGHT;

    private int yPosition = UI_HEIGHT;
    private int xPosition = UI_WIDTH;
    private Timer animationTimer; // 声明定时器

    public Main(int uiWidth, int uiHeight) {
        UI_WIDTH = uiWidth;
        UI_HEIGHT = uiHeight;
        setTitle("主页界面");
        setBounds(UI_WIDTH, UI_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT);
        // 设置无边框窗口
        setUndecorated(true);
        change();
        // 设置窗口始终在最上层
        setAlwaysOnTop(true);
    }

    public void scene(String img) {

        if (img == "/images/jump.gif") {
            startAnimation();
            CHANGE_TIME = 60000;
        }
        if (img == "/images/go.gif") {
            goAnimation();
            CHANGE_TIME = 60000;
        }
        // 创建面板
        JPanel panel = new JPanel();
        panel.setLayout(null);  // 使用绝对布局
        panel.setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT);

        // 加载图像
        URL url = Main.class.getResource(img);

        // 创建一个 ImageIcon 对象
        ImageIcon icon = new ImageIcon(url);

        JLabel label = new JLabel(icon);
        label.setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT);


        // 设置窗口和面板背景透明
        setBackground(new Color(0, 0, 0, 0));
        panel.setBackground(new Color(0, 0, 0, 0));

        // 将标签添加到面板
        panel.add(label);

        // 添加鼠标监听器以实现窗口移动
        MouseAdapter mouseAdapter = new MouseAdapter() {
            private Point initialClick;

            @Override
            public void mousePressed(MouseEvent e) {
                initialClick = e.getPoint();
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                if (initialClick != null) {
                    int thisX = getLocation().x + e.getX() - initialClick.x;
                    int thisY = getLocation().y + e.getY() - initialClick.y;
                    setLocation(thisX, thisY);
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    handleRightClick(e);
                }
            }

            private void handleRightClick(MouseEvent e) {
                Point location = getLocation();
                // 处理右键点击事件
                // 例如，显示一个弹出菜单
                JPopupMenu popupMenu = new JPopupMenu();
                JMenuItem item1 = new JMenuItem("关闭");
                JMenuItem item2 = new JMenuItem("菜单");

                // 为菜单项添加动作监听器
                item1.addActionListener(e1 -> {
                    System.exit(0);
                });
                item2.addActionListener(e1 -> {
                    setVisible(false);
                    new MenuGUI(location.x,location.y);

                });

                popupMenu.add(item1);
                popupMenu.add(item2);

                // 显示弹出菜单
                popupMenu.show(label, e.getX(), e.getY());
            }
        };

        // 为标签添加鼠标监听器
        label.addMouseListener(mouseAdapter);
        label.addMouseMotionListener(mouseAdapter);

        // 将面板添加到框架
        add(panel);

        // 刷新 GUI
        revalidate();
        repaint();
    }

    private void startAnimation() {
        // 定时器用于控制图片的移动
        animationTimer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Point location = getLocation();
                yPosition = location.y;
                yPosition -= 5; // 每次移动 10 像素
                setLocation(getX(), yPosition);
                if (yPosition + getHeight() < 0) {
                    yPosition = UI_HEIGHT; // 如果图片移出屏幕，重新从底部开始
                }
            }
        });
        animationTimer.start();
    }

    private void goAnimation() {
        // 定时器用于控制图片的移动
        animationTimer = new Timer(200, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Point location = getLocation();
                xPosition = location.x;
                xPosition -= 5; // 每次移动 5 像素
                setLocation(xPosition, getY());
                if (location.x < 0) {
                    xPosition = UI_WIDTH; // 如果图片移出屏幕，重新从底部开始
                    setLocation(xPosition, getY());
                }
            }
        });
        animationTimer.start();
    }

    private void change() {
        // 定时器用于控制图片的移动
        animationTimer = new Timer(CHANGE_TIME, new ActionListener() {//大概30分钟换照片
            // 打印窗口的位置
            @Override
            public void actionPerformed(ActionEvent e) {
                Point location = getLocation();
                int xPosition = location.x;
                int yPosition = location.y;
                setVisible(false);
                if (!hasVisibleWindows()) {
                    ImgController ImgController = new ImgController();
                    ImgController.Controller(xPosition, yPosition);
                }
            }
        });
        animationTimer.start();
    }

    public static boolean hasVisibleWindows() {
        // 获取所有窗口
        Window[] windows = Window.getWindows();

        // 检查每个窗口是否可见
        for (Window window : windows) {
            if (window.isVisible()) {
                return true;
            }
        }

        return false;
    }


}